//
//  MDContentView.swift
//  麦兜直播
//
//  Created by yeqingyu on 16/9/25.
//  Copyright © 2016年 yeqingyu. All rights reserved.
//

import UIKit

protocol MDContentViewDelegate:class {
    func MDContentViewDidMove(_ startPoint:CGPoint,offSet:CGPoint)
}

class MDContentView: UIView {
    
    var chiledControllers:[UIViewController]
    var contentCollectView:UICollectionView?
    weak var delegate:MDContentViewDelegate?
    var startPoint:CGPoint?
    
    
    init(frame: CGRect,chiledControllers:[UIViewController],parentController:UIViewController) {
        self.chiledControllers = chiledControllers
        super.init(frame: frame)
        setUpContentView()
        
        for vc in chiledControllers{
         parentController.addChildViewController(vc)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension MDContentView{
  fileprivate  func setUpContentView(){
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    layout.minimumLineSpacing = 0
    layout.minimumInteritemSpacing = 0
    layout.itemSize = CGSize(width: bounds.size.width, height: bounds.size.height)
    let contentCollectView = UICollectionView(frame: bounds, collectionViewLayout: layout)
    contentCollectView.backgroundColor = UIColor.white
    contentCollectView.showsHorizontalScrollIndicator = false
    contentCollectView.bounces = false
    contentCollectView.dataSource = self
    contentCollectView.delegate = self
    contentCollectView.isPagingEnabled = true
    self.contentCollectView = contentCollectView
    contentCollectView.register(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "content_cell")
    self.addSubview(contentCollectView)
    }
    
    
    
    func scrollToCurrrentPage(_ index:Int){
      let offPoint = CGPoint(x: CGFloat(index)*bounds.size.width, y: 0)
      contentCollectView?.setContentOffset(offPoint, animated: false)
    }
}


extension MDContentView:UICollectionViewDataSource,UICollectionViewDelegate{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.chiledControllers.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "content_cell", for: indexPath)
        // 2.给Cell设置内容
        let viewVc = self.chiledControllers[indexPath.row]
        viewVc.view.frame = cell.contentView.bounds
        cell.contentView.addSubview(viewVc.view)
        return cell
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        startPoint = scrollView.contentOffset
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        if scrollView.isDragging||scrollView.isDecelerating{
        delegate?.MDContentViewDidMove(startPoint!, offSet: scrollView.contentOffset)
        }

    }

}
